layui.use(['table','form','layer'], function() {
	var table = layui.table,table2=layui.table,
	form = layui.form,layer=layui.layer;

	/*维度信息表格*/
    table.render({
		elem: '#tabexampaper',
		id:"tabexampaperid",
		url: '../examinationpaper/getexaminationpaper',
		title: '试卷信息表',
		skin: 'line',
		height:'full-110',
		even: true,
		cols: [
			[{
				field: 'paperName',
				title: '试卷名称',
				align:'center'
			}, {
				field: 'paperDescribe',
				title: '试卷描述',
				align:'center'
			}, {
				field: 'usageLevel',
				title: '使用等级',
				align:'center'
			}, {
				field: 'usageType',
				title: '使用类型',
				toolbar : '#usageTypeBar',
				align:'center'
			}, {
				field: 'createTime',
				title: '创建时间',
				align:'center'
			}, {
				title: '操作',
				toolbar: '#barDemo',
				width: 180
			}]
		],
		page: {
			layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
			groups: 5,
			limit: 10,
			limits: [10, 20, 30, 40, 50],
			theme: '#1E9FFF',						
		}
	});

	/*点击查询对试卷进行筛选*/
	$("#btnselexampaper").click(function(){
		var papername=$("#inpselexampaper").val().trim();
		table.render({
			elem: '#tabexampaper',
			id:"tabexampaperid",
			url: '../examinationpaper/getexaminationpaper?papername='+papername,
			title: '试卷信息表',
			skin: 'line',
			height:'full-110',
			even: true,
			cols: [
				[{
					field: 'paperName',
					title: '试卷名称',
					align:'center'
				}, {
					field: 'paperDescribe',
					title: '试卷描述',
					align:'center'
				}, {
					field: 'usageLevel',
					title: '使用等级',
					align:'center'
				}, {
					field: 'usageType',
					title: '使用类型',
					align:'center'
				}, {
					field: 'createTime',
					title: '创建时间',
					align:'center'
				}, {
					title: '操作',
					toolbar: '#barDemo',
					width: 180
				}]
			],
			page: {
				layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
				groups: 5,
				limit: 10,
				limits: [10, 20, 30, 40, 50],
				theme: '#1E9FFF',						
			}
		});
	});
	
	/*添加试卷--弹窗*/
	$("#btnaddexampaper").click(function(){
		$("#inppaperName").val("");
		$("#txtpaperDescribe").val("");
		$("#inpusageLevel").val("");
		$("#inpchangedimensionids").val("");
		$("#inpchangedimension").val("");
		
		var index = layer.open({
	        type: 1, 
	        title: '量表试卷添加',
	        btn : [ '保存'],
	        shade: 0.8,
	        content: $('#addexappapercon'),
	        maxmin: true,
	        yes : function() {
				var inppaperName=$("#inppaperName").val().trim();   //试卷名称
				var txtpaperDescribe=$("#txtpaperDescribe").val().trim();   //试卷描述
				var inpusageLevel=$("#inpusageLevel").val().trim();     //使用等级   
				var inpchangedimensionids=$("#inpchangedimensionids").val().trim();
				var inpchangedimension=$("#inpchangedimension").val().trim();
				if(inppaperName==null || inppaperName==""){
					layer.msg("试卷名称不能为空",{icon:2});
					return false;
				}
				if(txtpaperDescribe==null || txtpaperDescribe==""){
					layer.msg("试卷描述不能为空",{icon:2});
					return false;
				}
				if(inpchangedimension==null || inpchangedimension==""){
					layer.msg("试卷维度不能为空",{icon:2});
					return false;
				}
				//遮罩层
	        	var indexsave = layer.load(0, {shade: 0.1}); 
				$.ajax({
        			url: '../examinationpaper/addexaminationpaper',  
        	        type: 'get', 
        	        dataType: 'json',  
        	        data:{papername:inppaperName,paperdescribe:txtpaperDescribe,
        	        	usageLevel:inpusageLevel,scaletitletypeid:inpchangedimensionids},
        	        success: function (getdata) {
        	          if (getdata.code == 10001) {
        	        	  layer.close(indexsave);
        	        	  layer.confirm(getdata.msg, {
	    					  btn: ['确定'],
	    					  icon:1
	    				  }, function(){
	    					  layer.closeAll();
	    					  table.reload("tabexampaperid", {
		    		                where: {
		    		                	keyword:getdata.code=='10001'
		    		                }
		    		          });	
	    				  });
        	          }else { 
        	        	  layer.alert(getdata.msg, {icon: 2});
        	          }  
        	        },
        	        error:function(){layer.alert("添加失败！", {icon:2});}
        		});
			}
	    });
    	layer.full(index);
	});
	
	/*点击选择试卷维度卷类--弹窗*/
	$("#btnadddimension").click(function(){
		/*维度卷类加载*/
		var dorderi=0;
		var indexload = layer.load(0, {shade: 0.1});
		$.ajax({
			type : 'get',
			url : '../scaletitletype/getallscaletitletype',
			datatype : 'json',
			success : function(dimension) {
				if (dimension.code == 0) {
					$("#tbodydimension").html("");
					var dimenisontitle="";
					var temp = [];
					var atr="";
					for(var i = 0; i < dimension.count; i++){
						if(temp.indexOf(dimension.data[i].dimensionName) == -1){
				            temp.push(dimension.data[i].dimensionName);
				            dorderi+=1;
				            var aradioscal="";
				            for(var j=0;j< dimension.count;j++){
				            	if(dimension.data[i].dimensionid==dimension.data[j].dimensionid){
				            		aradioscal+="<input name='item"+dorderi+"' type='radio' value='"+dimension.data[j].id+"' title='"+dimension.data[j].name+"' />"
				            	}
				            }
				            atr+="<tr><td>"+dimension.data[i].dimensionName+"</td><td>"+aradioscal+"</td></tr>";
						}
					}
					$("#tbodydimension").append(atr);
					form.render("radio");
				}
				layer.close(indexload);
				var index2=layer.open({
					type: 1, 
				    title: '选择量表维度卷类',
			        area: ['500px', '400px'],
			        shade: 0.8,
			        btn : [ '保存'],
			        content: $('#changedimensioncon'),
			        yes : function() {
			        	var dimensionids="";       //维度id
		  	        	var dimensionnamestext="";     //维度名称   
			            for(var i=1;i<=dorderi;i++){
			            	var adimensionids=$("input[name='item"+i+"']:checked").val();
			            	var adimensionnamestext=$("input[name='item"+i+"']").parent().siblings().text()
			            	if(adimensionids!=null || adimensionids!=undefined){
			            			dimensionnamestext+=adimensionnamestext+",";
				  	        		dimensionids+=adimensionids+","; 
			            	}
			            }
			            $("#inpchangedimensionids").val(dimensionids);
		  	        	$("#inpchangedimension").val(dimensionnamestext);
		  	        	layer.close(index2);
		  	        	$(".changedimension-con").css("display","none");
			            
					}
			    });
			},error:function(){layer.alert("维度卷类加载失败！", {icon:2});}
		});
		
  		
	});
	
	/*表格工具栏事件*/
    table.on('tool(tabexampaper)', function(obj){
	    var data = obj.data;
	    //点击查看
	    if(obj.event==='select'){
	    	$("#divpaperdetail").html("");
	    	$("#papercontainertitle").text(data.paperName);
	    	$("#papercontainerdescr").text(data.paperDescribe);
	    	$.ajax({
    			url: '../examinationpaper/getexaminationpapercontent',  
    	        type: 'get', 
    	        dataType: 'json',  
    	        data:{paperid:data.paperid},
    	        success: function (getdata) {
    	          if (getdata.code == 0) { 
    	        	  var getstr="";      //添加题目和选项
    	        	  for(var i=0;i<getdata.count;i++){
    	        		  var aoptiontitle=(i+1)+"."+getdata.data[i].titleName;   //获得题目
    	        		  var aoptionContent=getdata.data[i].optionContent;  //获得多个选项
    	        		  var optionContentstr=aoptionContent.split("&");    //得到一个选项
    	        		  var getoptioncontent="";
    	        		  for(var j=0;j<optionContentstr.length-1;j++){
    	        			  var newoptionContent=optionContentstr[j].split(",");   //得到一个选项的内容和分值
    	        			  var ABCoptionCotent=newoptionContent[0];     //某个选项内容
    	        			  var optioncontentscore=newoptionContent[1];   //某个选项分值
    	        			  getoptioncontent+="<li><input type='radio' value='"+optioncontentscore+"' name='item"+i+"' title='"+ABCoptionCotent+"' lay-skin='primary'></li>"
    	        		  }
    	        		  getstr+="<fieldset id='test_"+i+"'><legend><b>*</b><span>"+aoptiontitle+"</span>" +
    	        		  		"</legend><ul class='content-option'>"+getoptioncontent+"</ul></fieldset>";
    	        	  }
    	        	  $("#divpaperdetail").append(getstr);
    	        	  form.render();
    	          }else { 
    	        	  layer.alert(getdata.msg, {icon: 2});
    	          }  
    	        },
    	        error:function(){layer.alert(getdata.msg, {icon: 2});}
    		});
	    	var optionContent=data.optionContent;
	    	var index3=layer.open({
				type: 1, 
			    title: '量表信息详情',
		        shade: 0.8,
		        maxmin: true,
		        content: $('#papercontainer'),
		    });
	    	layer.full(index3);
			
	    }else if(obj.event==='delete'){
	    	layer.confirm('你确定要删除吗？', {
	    		  btn: ['确定','取消'] ,
	    		  icon:3
	    		}, function(){
	    	    	$.ajax({
	        			url: '../examinationpaper/deleteexaminationpaper',  
	        	        type: 'get', 
	        	        dataType: 'json',  
	        	        data:{paperid:data.paperid},
	        	        success: function (getdata) {
	        	          if (getdata.code == 10001) { 
	        	        	  layer.confirm(getdata.msg, {
		    					  btn: ['确定'],
		    					  icon:1
		    				  }, function(){
		    					  layer.closeAll();
		    					  table.reload("tabexampaperid", {
			    		                where: {
			    		                	keyword:getdata.code=='10001'
			    		                }
			    		          });	
		    				  });
	        	          }else { 
	        	        	  layer.alert(getdata.msg, {icon: 2});
	        	          }  
	        	        },
	        	        error:function(){layer.alert(getdata.msg, {icon: 2});}
	        		});
	    		}, function(){}
	    	);
	    }
    });
    
    
    
});